/**
 * 登录页面
 * @author yaohuan
 * @date 2019/1/11
 */
define(function () {
    let looked = true,
        remember = true
    ;

    $(function ($) {
        var $dom = $(document),
            $password = $('#password'),
            $username = $('#username'),
            login = function () {
                app.ajax({
                    xurl: 'toLogin',
                    xdata: {
                        username: $username.val(),
                        password: $password.val(),
                        remember: false,
                        dbc_: '80',
                        lang_: 'zh_CN'
                    },
                    loading: true,
                    rules: {username: {nul: [false, '账号不能为空']}, password: {nul: [false, '密码不能为空']}},
                    cb: function (result) {
                        if (result.ok) {
                            if (remember) {
                                app.lStorage.set('crm-userlogin', result.xdata, '1M');
                            }
                            location.href = g_ctxPath + 'index';
                        } else {
                            app.showError('账号密码不正确,请检查登录信息！');
                        }
                    }
                });
            }
        ;

        let loginCache = app.lStorage.get('crm-userlogin');
        if(loginCache){
            $password.val(loginCache.password);
            $username.val(loginCache.username);
        }

        $("#login").click(function () {
            login();
        });

        //密码
        $("#passIco").click(function () {
            if (looked) {
                $(this).addClass('icon-01');
                $password.attr('type', 'text');
                looked = false;
            } else {
                $(this).removeClass('icon-01');
                $password.attr('type', 'password');
                looked = true;
            }
            return false;
        });

        //处理辅助功能
        $username.keypress(function (event) {
            if (event.keyCode == 13) {
                $password.focus();
                return false;
            }
        });

        $password.keypress(function (event) {
            if (event.keyCode == 13) {
                if ($username.val() == '') {
                    $username.focus();
                } else {
                    login();
                }
                return false;
            }
        });

        $dom.keypress(function (event) {
            if (event.keyCode == 13) {
                login();
                return false;
            }

        });

        layui.use(['form'], function () {
            let form = layui.form;
            form.on('checkbox(filter)', function (data) {
                remember = data.elem.checked;
            });

            layui.use(['table'], $.noop);
        });

    })
});